package com.example.book_manage_240827.controller;



import com.example.book_manage_240827.constonts.Constants;
import com.example.book_manage_240827.model.*;
import com.example.book_manage_240827.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping(value = "/addBook")
    public String addBook(BookInfo bookInfo) {
        log.info("添加图书, bookInfo:{}", bookInfo);
        //参数校验
        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() <= 0
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())) {
            return "参数错误";
        }
        //添加图书
        try {
            bookService.insertBook(bookInfo);
            return "";
        } catch (Exception e) {
            log.error("添加图书失败, e:{}", e);
            return e.getMessage();
        }
    }

    @RequestMapping("/getListByPage")
    public Result getListByPage(PageRequest pageRequest, HttpSession session) {
        log.info("获取图书列表, pageRequest:{}", pageRequest);
        log.info("session", session);

        //判断⽤⼾是否登录
        if (session.getAttribute(Constants.USER_SESSION_KEY)==null){
            return Result.unlogin();
        }
        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.USER_SESSION_KEY);
        if (userInfo==null || userInfo.getId()<0 ||
                "".equals(userInfo.getUserName())){
            return Result.unlogin();
        }
        //⽤⼾登录, 返回图书列表
        PageResult<BookInfo> pageResult =
                bookService.getListByPage(pageRequest);
        return Result.success(pageResult);
    }

//    @RequestMapping("/getListByPage")
//    public PageResult<BookInfo> getListByPage(PageRequest pageRequest) {
//        log.info("获取图书列表, pageRequest:{}", pageRequest);
//        PageResult<BookInfo> pageResult =
//                bookService.getListByPage(pageRequest);
//        return pageResult;
//    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
        if (bookId==null || bookId<=0){
            return new BookInfo();
        }
        BookInfo bookInfo = bookService.queryBookById(bookId);
        return bookInfo;
    }
    @RequestMapping("/updateBook")
    public String updateBook(BookInfo bookInfo) {
        log.info("修改图书:{}", bookInfo);
        try {
            bookService.updateBook(bookInfo);
            return "";
        } catch (Exception e) {
            log.error("修改图书失败", e);
            return e.getMessage();
        }
    }


    /**
     * 批量删除
     */
    @RequestMapping("/batchDelete")
    public boolean batchDelete(@RequestParam List<Integer> ids){
        log.info("批量删除数据, ids:{}", ids);
        try {
            Integer result = bookService.batchDelete(ids);
        }catch (Exception e){
            log.error("批量删除数据失败, ids:{}, e:{}", ids, e);
            return false;
        }
        return true;
    }

}

